package com.dream.dream.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dream.dream.domain.Ssq;
import com.dream.dream.dto.SsqDto;
import com.dream.dream.service.SsqService;
import com.dream.dream.mapper.SsqMapper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 剪辑
 * @description 针对表【dlt】的数据库操作Service实现
 * @createDate 2024-09-23 09:01:06
 */
@Service
public class SsqServiceImpl extends ServiceImpl<SsqMapper, Ssq>
        implements SsqService {

    public final static int limit = 500;

    public static int allPage;

    public final String url = "http://api.huiniao.top/interface/home/lotteryHistory?type=ssq&page= ";

    @Autowired
    RestTemplate restTemplate;


    /**
     * 获取最新数据
     */
    @Override
    public void getData() {
        // 获取所有数据
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("Content-Type", "application/json;charset=UTF-8;");
        HttpEntity httpEntity = new HttpEntity(null, httpHeaders);
        ResponseEntity<String> exchange = restTemplate.exchange(url + 1 + " &limit=" + limit, HttpMethod.GET, httpEntity, String.class);
        JSONObject jsonObject = JSONObject.fromObject(exchange.getBody());
        JSONObject object = jsonObject.getJSONObject("data").getJSONObject("last");
        Ssq ssq = new Ssq();
        ssq.setCode(object.getString("code"));
        ssq.setDay(object.getString("day"));
        ssq.setOne(object.getString("one"));
        ssq.setTwo(object.getString("two"));
        ssq.setThree(object.getString("three"));
        ssq.setFour(object.getString("four"));
        ssq.setFive(object.getString("five"));
        ssq.setSix(object.getString("six"));
        ssq.setSeven(object.getString("seven"));
        ssq.setOpenTime(object.getString("open_time"));
        ssq.setNextOpenTime(object.getString("next_open_time"));
        ssq.setNextCode(object.getString("next_code"));
        this.save(ssq);
    }

    /**
     * 获取传入page获取数据
     *
     * @return
     */
    @Override
    public int getAllData() {
        //获取第一页数据，以及总页数
        for (int currentPage = get(1, limit) + 1; currentPage <= allPage; currentPage++) {
            get(currentPage, limit);
        }
        return 0;
    }


    private int get(int totalPage, int limit) {
        // 获取所有数据
        List<Ssq> list = new ArrayList<>();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("Content-Type", "application/json;charset=UTF-8;");
        HttpEntity httpEntity = new HttpEntity(null, httpHeaders);
        ResponseEntity<String> exchange = restTemplate.exchange(url + totalPage + " &limit=" + limit, HttpMethod.GET, httpEntity, String.class);
        JSONObject jsonObject = JSONObject.fromObject(exchange.getBody());
        JSONObject object = jsonObject.getJSONObject("data").getJSONObject("data");
        //todo  这里直接放开会报错 null object 每次请求后停顿1S就正常
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        List<SsqDto> lists = JSONArray.toList(object.getJSONArray("list"), SsqDto.class);
        int currentPage = Integer.parseInt((String) object.get("currentPage")); //当前页
        allPage = (int) object.get("totalPage");           //总页数
        for (SsqDto d : lists) {
            Ssq ssq = new Ssq();
            ssq.setCode(d.getCode());
            ssq.setDay(d.getDay());
            ssq.setOne(d.getOne());
            ssq.setTwo(d.getTwo());
            ssq.setThree(d.getThree());
            ssq.setFour(d.getFour());
            ssq.setFive(d.getFive());
            ssq.setSix(d.getSix());
            ssq.setSeven(d.getSeven());
            ssq.setOpenTime(d.getOpen_time());
            ssq.setNextOpenTime(d.getNext_open_time());
            ssq.setNextCode(d.getNext_code());
            list.add(ssq);
        }
        this.saveBatch(list);
        return currentPage;
    }


}




